dnl **************************************************************************
dnl * Initialize
dnl **************************************************************************
AC_INIT([[S2E]],[[0.01]],[vitaly.chipounov@epfl.ch])

dnl Identify where LLVM source tree is (this is patched by
dnl AutoRegen.sh)
LLVM_SRC_ROOT=XXX

dnl Tell autoconf that the auxiliary files are actually located in
dnl the LLVM autoconf directory, not here.
AC_CONFIG_AUX_DIR($LLVM_SRC_ROOT/autoconf)

dnl Tell autoconf that this is an LLVM project being configured
dnl This provides the --with-llvmsrc and --with-llvmobj options
LLVM_CONFIG_PROJECT("","")

dnl Verify that the source directory is valid
AC_CONFIG_SRCDIR(["Makefile.config.in"])

dnl Configure a common Makefile
AC_CONFIG_FILES(Makefile.config)

dnl Configure project makefiles
dnl List every Makefile that exists within your source tree
AC_CONFIG_HEADERS([include/s2etools/config.h])

dnl FIXME: Make out of tree builds work.
AC_CONFIG_MAKEFILE(Makefile)
AC_CONFIG_MAKEFILE(Makefile.common)

dnl Do the first stage of configuration for s2etools-config.in.
AC_CONFIG_FILES([tools/s2etools-config/s2etools-config.in])

AC_LANG([C++])

dnl **************************************************************************
dnl Find the host

AC_CANONICAL_TARGET

dnl Determine the platform type and cache its value. This helps us configure
dnl the System library to the correct build platform.
AC_CACHE_CHECK([type of operating system we're going to host on],
               [s2etools_cv_os_type],
[case $host in
  *-*-linux*)
    host_supports_posix_runtime=yes ;;
  *)
    host_supports_posix_runtime=no ;;    
esac])

OS=`uname -a`
case "$OS" in
  MINGW*) OS=mingw
	;;
  mingw*) OS=mingw
  ;;
  Linux*) OS=linux
  ;;
  *) OS=
  ;;
esac

dnl **************************************************************************
dnl Verify that we can find s2e

AC_ARG_WITH(s2esrc,
  AS_HELP_STRING([--with-s2esrc],
    [Location of S2E source code]),,)

dnl Make the paths absolute.
s2e_src=`cd $with_s2esrc 2> /dev/null; pwd`
   
AC_MSG_RESULT([$s2e_src])
AC_SUBST(S2E_SRC,$s2e_src)

dnl CXXFLAGS
AC_SUBST(CXXFLAGS, "$CXXFLAGS -fexceptions")

dnl **************************************************************************
dnl Header include paths

AC_ARG_WITH(bfd-incdir,
  AS_HELP_STRING([--with-bfd-incdir],
    [Location of BFD headers]),,)

AC_ARG_WITH(libiberty-incdir,
  AS_HELP_STRING([--with-libiberty-incdir],
    [Location of libiberty headers]),,)

if test X${with_bfd_incdir} != X; then
    AC_SUBST(CXXFLAGS, "$CXXFLAGS -I\"${with_bfd_incdir}\"")
fi

if test X${with_libiberty_incdir} != X; then
    AC_SUBST(CXXFLAGS, "$CXXFLAGS -I\"${with_libiberty_incdir}\"")
fi

dnl **************************************************************************
dnl Library paths

AC_ARG_WITH(bfd-libdir,
  AS_HELP_STRING([--with-bfd-libdir],
    [Location of BFD libraries]),,)

AC_ARG_WITH(libiberty-libdir,
  AS_HELP_STRING([--with-libiberty-libdir],
    [Location of libiberty libraries]),,)

if test X${with_bfd_libdir} != X; then
    AC_SUBST(LDFLAGS, "$LDFLAGS -L\"${with_bfd_libdir}\"")
fi

if test X${with_libiberty_libdir} != X; then
    AC_SUBST(LDFLAGS, "$LDFLAGS -L\"${with_libiberty_libdir}\"")
fi

dnl **************************************************************************
dnl Add OS-specific libraries

echo "$OS"
if test "x$OS" = "xmingw" ; then
tool_libs="-lbfd -lintl -liberty -lz"
elif test "x$OS" = "xlinux" ; then
tool_libs="-lbfd -liberty -lz -lgettextpo"
else
tool_libs="-lbfd -lintl -liberty -lz -lgettextpo"
fi

AC_SUBST(TOOL_LIBS,$tool_libs)


dnl **************************************************************************
dnl Verify that we can find llvm

dnl --with-llvm is a shortcut for setting srcdir and objdir.
AC_ARG_WITH(llvm,
  AS_HELP_STRING([--with-llvm],
    [Location of LLVM Source and Object code]),,)

AC_MSG_CHECKING([llvm source dir])

if test X${with_llvm} != X; then
    dnl Verify that --with-llvm{src,obj} were not given.
    if test X${with_llvmsrc} != X; then
       AC_MSG_ERROR([--with-llvmsrc cannot be specified when using --with-llvm])
    fi   
    if test X${with_llvmobj} != X; then
       AC_MSG_ERROR([--with-llvmobj cannot be specified when using --with-llvm])
    fi   
    with_llvmsrc=$with_llvm
    with_llvmobj=$with_llvm
fi

dnl If one of with_llvmsrc or with_llvmobj was given, we must have both.
if (test X${with_llvmsrc} != X || test X${with_llvmobj} != X); then
    dnl Verify that with_llvmobj was given as well.
    if test X${with_llvmsrc} = X; then
       AC_MSG_ERROR([--with-llvmsrc must be specified when using --with-llvmobj])
    fi      
    if test X${with_llvmobj} = X; then
       AC_MSG_ERROR([--with-llvmobj must be specified when using --with-llvmsrc])
    fi      
else
    dnl Otherwise try and use llvm-config to find.
    llvm_version=`llvm-config --version`
    if test X${llvm_version} = X; then
       AC_MSG_ERROR([unable to find llvm, use --with-llvmsrc and --with-llvmobj])
    fi
    
    with_llvmsrc=`llvm-config --src-root`
    with_llvmobj=`llvm-config --obj-root`
fi

dnl Try to validate directories.
if test ! -f ${with_llvmsrc}/Makefile.rules; then
   AC_MSG_ERROR([invalid llvmsrc directory: ${with_llvmsrc}])
fi
if test ! -f ${with_llvmobj}/Makefile.config; then
   AC_MSG_ERROR([invalid llvmobj directory: ${with_llvmobj}])
fi

dnl Make the paths absolute.
llvm_src=`cd $with_llvmsrc 2> /dev/null; pwd`
llvm_obj=`cd $with_llvmobj 2> /dev/null; pwd`
   
AC_MSG_RESULT([$llvm_src])

dnl Report obj dir as well.
AC_MSG_CHECKING([llvm obj dir])
AC_MSG_RESULT([$llvm_obj])

AC_SUBST(LLVM_SRC,$llvm_src)
AC_SUBST(LLVM_OBJ,$llvm_obj)

dnl Determine LLVM version.
AC_MSG_CHECKING([llvm package version])
llvm_package_version=`grep PACKAGE_VERSION= $with_llvmsrc/configure | cut -d\' -f 2`
AC_MSG_RESULT([$llvm_package_version])

llvm_version_split=`python -c "import re; print('\t'.join(map(str, re.match('([[0-9]]+)[.]([[0-9]]+)(svn)?', \"$llvm_package_version\").groups())))"`

AC_MSG_CHECKING([llvm version major])
llvm_version_major=`echo "$llvm_version_split" | cut -f 1`
AC_MSG_RESULT([$llvm_version_major])

AC_MSG_CHECKING([llvm version minor])
llvm_version_minor=`echo "$llvm_version_split" | cut -f 2`
AC_MSG_RESULT([$llvm_version_minor])

AC_MSG_CHECKING([llvm is release version])
llvm_version_svn=`echo "$llvm_version_split" | cut -f 3`
if test "$llvm_version_svn" == "svn"; then
  llvm_is_release=0
else
  llvm_is_release=1
fi
AC_MSG_RESULT([$llvm_is_release])

AC_DEFINE_UNQUOTED(LLVM_VERSION_MAJOR, $llvm_version_major, [LLVM major version number])
AC_SUBST(LLVM_VERSION_MAJOR,$llvm_version_major)
AC_DEFINE_UNQUOTED(LLVM_VERSION_MINOR, $llvm_version_minor, [LLVM minor version number])
AC_SUBST(LLVM_VERSION_MINOR,$llvm_version_minor)
AC_DEFINE_UNQUOTED(LLVM_IS_RELEASE, $llvm_is_release, [LLVM version is release (instead of development)])
AC_SUBST(LLVM_IS_RELEASE,$llvm_is_release)

dnl Look for a sufficiently recent version of Perl.
LLVM_PROG_PERL([5.006])
AC_SUBST(PERL)
if test x"$PERL" = xnone; then
  AC_SUBST(HAVE_PERL,0)
  AC_MSG_ERROR([perl is required but was not found, please install it])
else
  AC_SUBST(HAVE_PERL,1)
fi


dnl **************************************************************************
dnl Checks for header files.

dnl NOTE: This is mostly just to force autoconf to make CFLAGS defines
dnl for us.
AC_LANG_PUSH([C])

AC_CHECK_HEADERS([sys/acl.h])

AC_LANG_POP([C])

AC_CHECK_HEADERS([selinux/selinux.h],
        AC_SUBST(HAVE_SELINUX, 1),
        AC_SUBST(HAVE_SELINUX, 0))


# Translate the various configuration directories and other basic
# information into substitutions that will end up in Makefile.config.in
# that these configured values can be used by the makefiles
if test "${prefix}" = "NONE" ; then
      prefix="/usr/local"
fi
eval S2ETOOLS_PREFIX="${prefix}";
S2ETOOLS_CONFIGTIME=`date`
AC_SUBST(S2ETOOLS_PREFIX)
AC_SUBST(S2ETOOLS_CONFIGTIME)

dnl **************************************************************************
dnl * Create the output files
dnl **************************************************************************

dnl This must be last
AC_OUTPUT
